package com.example.roller

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.roller.ui.theme.RollerTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            RollerTheme {
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    ArtGalleryScreen()
                }
            }
        }
    }
}

@Composable
fun ArtGalleryScreen() {
    val artworks = listOf(
        Artwork(R.drawable.my_artwork_1, "Blue Rose and Flowers", "Owen Scott", 2021),
        Artwork(R.drawable.my_artwork_2, "Sunflower Field", "Emma Wilson", 2020),
        Artwork(R.drawable.my_artwork_3, "Mountain Landscape", "James Brown", 2019)
    )

    var currentIndex by remember { mutableIntStateOf(0) }
    val artwork = artworks[currentIndex]

    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(
                brush = Brush.verticalGradient(
                    colors = listOf(
                        Color(0xFF1a2a3a),
                        Color(0xFF0d1b2a)
                    )
                )
            )
            .padding(24.dp),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        // 标题区域
        Text(
            text = "🎨 Art Gallery",
            fontSize = 32.sp,
            fontWeight = FontWeight.Bold,
            color = Color.White,
            modifier = Modifier.padding(bottom = 8.dp)
        )

        Text(
            text = "艺术作品展示",
            fontSize = 16.sp,
            color = Color(0xFFaaaaaa),
            modifier = Modifier.padding(bottom = 24.dp)
        )

        // 图片展示区域 - 使用卡片增强视觉效果
        Card(
            modifier = Modifier
                .weight(0.7f)
                .fillMaxWidth()
                .shadow(
                    elevation = 16.dp,
                    shape = RoundedCornerShape(20.dp),
                    clip = false
                ),
            shape = RoundedCornerShape(20.dp),
            colors = CardDefaults.cardColors(containerColor = Color.White),
            elevation = CardDefaults.cardElevation(defaultElevation = 8.dp)
        ) {
            Box(
                modifier = Modifier.fillMaxSize(),
                contentAlignment = Alignment.Center
            ) {
                Image(
                    painter = painterResource(artwork.imageRes),
                    contentDescription = artwork.title,
                    contentScale = ContentScale.Crop,
                    modifier = Modifier
                        .fillMaxSize(0.9f)
                        .clip(RoundedCornerShape(16.dp))
                )
            }
        }

        Spacer(modifier = Modifier.height(24.dp))

        // 作品信息区域 - 使用卡片包装
        Card(
            modifier = Modifier
                .weight(0.2f)
                .fillMaxWidth(),
            shape = RoundedCornerShape(16.dp),
            colors = CardDefaults.cardColors(containerColor = Color(0xFF2a3a4a))
        ) {
            Column(
                modifier = Modifier
                    .fillMaxSize()
                    .padding(20.dp),
                horizontalAlignment = Alignment.CenterHorizontally,
                verticalArrangement = Arrangement.Center
            ) {
                Text(
                    text = artwork.title,
                    fontSize = 22.sp,
                    fontWeight = FontWeight.Bold,
                    color = Color.White,
                    textAlign = TextAlign.Center,
                    lineHeight = 28.sp
                )

                Spacer(modifier = Modifier.height(8.dp))

                Text(
                    text = "${artwork.artist} · ${artwork.year}",
                    fontSize = 16.sp,
                    color = Color(0xFFaaaaaa),
                    fontWeight = FontWeight.Medium
                )
            }
        }

        Spacer(modifier = Modifier.height(24.dp))

        // 控制按钮区域
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .weight(0.1f),
            horizontalArrangement = Arrangement.SpaceBetween
        ) {
            // 上一张按钮
            Button(
                onClick = {
                    currentIndex = if (currentIndex > 0) currentIndex - 1 else artworks.size - 1
                },
                modifier = Modifier
                    .weight(1f)
                    .padding(horizontal = 4.dp),
                colors = ButtonDefaults.buttonColors(
                    containerColor = Color(0xFF4a6572)
                ),
                shape = RoundedCornerShape(12.dp)
            ) {
                Text(
                    text = "← Previous",
                    fontSize = 14.sp,
                    fontWeight = FontWeight.Medium
                )
            }

            // 随机按钮
            Button(
                onClick = {
                    var newIndex: Int
                    do {
                        newIndex = (0 until artworks.size).random()
                    } while (newIndex == currentIndex)
                    currentIndex = newIndex
                },
                modifier = Modifier
                    .weight(1f)
                    .padding(horizontal = 4.dp),
                colors = ButtonDefaults.buttonColors(
                    containerColor = Color(0xFFf9a825)
                ),
                shape = RoundedCornerShape(12.dp)
            ) {
                Text(
                    text = "🎲 Random",
                    fontSize = 14.sp,
                    fontWeight = FontWeight.Medium
                )
            }

            // 下一张按钮
            Button(
                onClick = {
                    currentIndex = (currentIndex + 1) % artworks.size
                },
                modifier = Modifier
                    .weight(1f)
                    .padding(horizontal = 4.dp),
                colors = ButtonDefaults.buttonColors(
                    containerColor = Color(0xFF4a6572)
                ),
                shape = RoundedCornerShape(12.dp)
            ) {
                Text(
                    text = "Next →",
                    fontSize = 14.sp,
                    fontWeight = FontWeight.Medium
                )
            }
        }

        // 作品计数器
        Text(
            text = "作品 ${currentIndex + 1} / ${artworks.size}",
            fontSize = 14.sp,
            color = Color(0xFF888888),
            modifier = Modifier.padding(top = 16.dp)
        )
    }
}

// 数据类
data class Artwork(
    val imageRes: Int,
    val title: String,
    val artist: String,
    val year: Int
)